﻿namespace MA.GoogleInc
{
    using Google.GData.Analytics;
    using Google.GData.Client;
    using System;
    using System.Text;
    using System.Xml.Linq;
    using System.Collections.ObjectModel;
    using System.Linq;

    public partial class Analytic
    {

        private DataFeed _feed;
        private DataQuery _Dq;

        public DataQuery DataQuery
        {
            get { return _Dq; }
            set { _Dq = value; }
        }
        private DataFeed Feed
        {
            get { return _feed; }
            set { _feed = value; }
        }
        private DataQuery dataQuery(PropAnalytic PropertiesAnalytic, string IDtable)
        {
            String baseUrl = "https://www.google.com/analytics/feeds/data";
            DataQuery query = new DataQuery(baseUrl);

            query.Dimensions = PropertiesAnalytic.Dimensions;
            query.Metrics = PropertiesAnalytic.Metrics;
            query.Segment = PropertiesAnalytic.Segment;
            query.Filters = PropertiesAnalytic.Filters;
            query.Sort = PropertiesAnalytic.Sort;
            query.GAStartDate = PropertiesAnalytic.StartDate;
            query.GAEndDate = PropertiesAnalytic.EndtDate;
            query.Ids = IDtable;

            return query;
        }

        public Analytic() { }
        public Analytic(CredentialsAnalytic credentialsAnalytic, PropAnalytic PropertiesAnalytic)
        {
            try
            {
                AnalyticsService asv = new AnalyticsService("gaExportAPI_acctSample_v2.0");
                asv.setUserCredentials(credentialsAnalytic.User, credentialsAnalytic.Pass);
                Feed = asv.Query(dataQuery(PropertiesAnalytic, credentialsAnalytic.IDTable));
            }
            catch (Exception)
            {
                throw;
            }
        }


        public string GetTotalVisitors()
        {
            StringBuilder oReturn = new StringBuilder();
            Aggregates aggregates = Feed.Aggregates;

            foreach (Metric metric in aggregates.Metrics)
            {
                oReturn.Append(metric.Name + " : " + metric.Value);
                oReturn.Append(Environment.NewLine);
            }

            return oReturn.ToString();
        }

        public string GetVisitors()
        {
            StringBuilder oReturn = new StringBuilder();
            string res = string.Empty;
            int d = 0;

            foreach (AtomEntry Result in Feed.Entries)
            {
                DataEntry Entry = (DataEntry)Result;
                d = d + Convert.ToInt32(Entry.Metrics[0].Value);
            }
            oReturn.Append(d.ToString());

            return oReturn.ToString();
        }

        public string GetVisits()
        {
            StringBuilder oReturn = new StringBuilder();
            string res = string.Empty;
            int d = 0;

            foreach (AtomEntry Result in Feed.Entries)
            {
                DataEntry Entry = (DataEntry)Result;
                d = d + Convert.ToInt32(Entry.Metrics[1].Value);
            }
            oReturn.Append(d.ToString());

            return oReturn.ToString();
        }

        public string GetNewVisits()
        {
            StringBuilder oReturn = new StringBuilder();
            string res = string.Empty;
            int d = 0;

            foreach (AtomEntry Result in Feed.Entries)
            {
                DataEntry Entry = (DataEntry)Result;
                d = d + Convert.ToInt32(Entry.Metrics[2].Value);
            }
            oReturn.Append(d.ToString());

            return oReturn.ToString();
        }

        public string GetLatLogResultJsonString()
        {
            StringBuilder oReturn = new StringBuilder();
            Aggregates aggregates = Feed.Aggregates;

            int ban = 0;

            oReturn.Append("{\"GEO\":[");

            foreach (AtomEntry Result in Feed.Entries)
            {
                DataEntry Entry = (DataEntry)Result;
                if (ban == 0)
                {
                    ban = 1;
                    oReturn.Append("{" + "\"Lat\":" + "\"" + Entry.Dimensions[0].Value.ToString() + "\","
                                       + "\"Log\":" + "\"" + Entry.Dimensions[1].Value.ToString() + "\","
                                       + "\"Pais\":" + "\"" + Entry.Dimensions[2].Value.ToString() + "\","
                                       + "\"Ciudad\":" + "\"" + Entry.Dimensions[3].Value.ToString() + "\","
                                       + "\"Ciudad\":" + "\"" + Entry.Dimensions[5].Value.ToString() + "\","
                                       + "\"Visitors\":" + "\"" + Entry.Metrics[0].Value.ToString() + "\"" + "}");
                }
                else
                {
                    oReturn.Append(",{" + "\"Lat\":" + "\"" + Entry.Dimensions[0].Value.ToString() + "\","
                                        + "\"Log\":" + "\"" + Entry.Dimensions[1].Value.ToString() + "\","
                                        + "\"Pais\":" + "\"" + Entry.Dimensions[2].Value.ToString() + "\","
                                        + "\"Ciudad\":" + "\"" + Entry.Dimensions[3].Value.ToString() + "\","
                                        + "\"Ciudad\":" + "\"" + Entry.Dimensions[5].Value.ToString() + "\","
                                        + "\"Visitors\":" + "\"" + Entry.Metrics[0].Value.ToString() + "\"" + "}");
                }

            }

            oReturn.Append("]}");

            return oReturn.ToString();
        }

    }

    public class PropAnalytic
    {
        private string _startDate;
        private string _endtDate;
        private string _dimensions;
        private string _metrics;
        private string _segment;
        private string _filters;
        private string _sort;

        public string Sort
        {
            get { return _sort; }
            set { _sort = value; }
        }
        public string Filters
        {
            get { return _filters; }
            set { _filters = value; }
        }
        public string Segment
        {
            get { return _segment; }
            set { _segment = value; }
        }
        public string Metrics
        {
            get { return _metrics; }
            set { _metrics = value; }
        }
        public string Dimensions
        {
            get { return _dimensions; }
            set { _dimensions = value; }
        }
        public string EndtDate
        {
            get { return _endtDate; }
            set { _endtDate = value; }
        }
        public string StartDate
        {
            get { return _startDate; }
            set { _startDate = value; }
        }

    }

    public class CredentialsAnalytic
    {
        private string _User;
        private string _Pass;
        private string _IdTable;

        public string IDTable
        {
            get { return _IdTable; }
            set { _IdTable = value; }
        }
        public string Pass
        {
            get { return _Pass; }
            set { _Pass = value; }
        }
        public string User
        {
            get { return _User; }
            set { _User = value; }
        }

    }

}
